﻿var userFilterType;
var userFilterValue;
var isManager = null;
function setAvailableApplications(response)
{
	$('#sortable1').delay(700).empty();
	$('.categories').empty();
	var selectedCat;
	if (response.length === 0)
	{
		$('div.available-apps').children('div').addClass('no-result');
		$('<div><span>Veuillez changer le filtre pour séléctionner des applications.</span></div>').appendTo('#sortable1');
	}
	else
	{
		if ($('div.available-apps').children('div').hasClass('no-result'))
		{
			$('div.available-apps').children('div').removeClass('no-result');
		}
		for (var i = 0; i < response.length; i++)
		{
			var categoryId = response[i].Categorie.replace(/\s/g, '');
			var categoryFound = false;
			$('.categories').children('div').each(function ()
			{
				if ($(this).attr('id') === categoryId)
				{
					categoryFound = true;
				}
			});
			if (categoryFound === false)
			{
				var categorie = $('<div id=' + categoryId + '><span>' + response[i].Categorie + '</span></div>').bind('click', selectCategorie);
				categorie.appendTo('.categories');
				if (i === 0)
				{
					selectedCat = categoryId;
					categorie.addClass('selected');
				}
			}
			var newItem = $('<li id="' + response[i].Id + '" class="grab" categorie="' + categoryId + '"><div>' + response[i].Name + '</div></li>');
			if (response[i].Description !== null && response[i].Description !== "")
			{
				newItem.attr('description', response[i].Description);
				newItem.bind('mouseover', function ()
				{
					var desc = $(this).attr('description');
					$(this).append("<div class='info' style='position:absolute;top:0'>" + response[i].Name + "</div>");
				});
				newItem.bind('mouseout', function ()
				{
					$(this).children('div.info').remove();
				});
			}
			if (categoryId !== selectedCat)
			{
				newItem.appendTo('#sortable1').hide();
			}
			else
			{
				newItem.hide().appendTo('#sortable1').slideDown("slow");
			}
		}
	}
}
function initTeamsFilters(selectedTeam)
{
	callApplicationPageSelectedApps({ search: 'direction' }, function (response)
	{
		for (var i = 0; i < response.length; i++)
		{
			if (selectedTeam.toUpperCase() == response[i].Name.toUpperCase())
			{
				$('#direction-filter').append("<option value='" + response[i].Name + "' selected='selected'>" + response[i].Name + "</option>");
			}
			else
			{
				$('#direction-filter').append("<option value='" + response[i].Name + "'>" + response[i].Name + "</option>");
			}
		}
		$('#direction-filter').append($("<option value='AUTRES'>AUTRES</option>"));
	});
}
function getFilterValues()
{
	if ($("input#filterType").val() !== null
		&& $("input#filterValue").val() !== null)
	{
		userFilterType = $('input#filterType').val();
		userFilterValue = $('input#filterValue').val();
	}
	if ($("input#isManager").val() !== null)
	{
		if ($("input#isManager").val() == 'true')
		{
			isManager = true;
		}
	}
}
function getAvailableApplications()
{
	var defaultDataParameters = { search: 'application' };
	callApplicationPageSelectedApps(defaultDataParameters, setAvailableApplications);
}
function callApplicationPageByUser(dataParameters, f)
{
	$.ajax({
		type: "GET",
		url: "/_layouts/15/VPLink2014/MesApplications/ApplicationPages/ApplicationOperationsByUser.aspx",
		dataType: "json",
		cache: false,
		data: dataParameters,
		success: function (response) { f(response); }
	});
}

function callApplicationPageSelectedApps(dataParameters, f)
{
	$.ajax({
		type: "GET",
		url: "/_layouts/15/VPLink2014/MesApplications/ApplicationPages/ApplicationOperationsByManager.aspx",
		dataType: "json",
		cache: false,
		data: dataParameters,
		success: function (response) { f(response); }
	});
}
